package com.touch.server;
import java.io.File;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Random;
import java.util.Vector;

import com.touch.account.Account;
import com.touch.core.job.Job;
import com.touch.core.net.UDPServer;
import com.touch.core.util.IOTools;
public class ServerControls 
{
	public static UDPServer SERVER_SOCKET;
	public static HashMap <String, Account> AccountMap = new HashMap <String, Account>();
	public static HashMap <String, Job> JobMap = new HashMap <String, Job>();
	public static Random randomizer = new Random();
	public static void checkJobs()
	{
		System.out.println("Checking Classes...");
		File dir = new File("Classes");
		if(!dir.exists())
			dir.mkdir();					
	}
	public static void loadJobs()
	{
		System.out.println("Loading classes...");
		Vector<File> files = IOTools.getFiles("Classes", ".dat");
		if(files == null)
			return;
		for(File file: files)
		{
			Job j = Job.loadClass(file.getAbsolutePath());
			System.out.println("    loading: " + j.name);
			JobMap.put(j.name, j);
		}
	}
	public static void startServer() throws SocketException
	{
		System.out.println("Starting server...");
		int port = 62594;
		SERVER_SOCKET = new UDPServer(port);
		SERVER_SOCKET.addNetListener(new ServerSocketEvents());
		System.out.println("Server ready!");
	}
	public static int randomInt(int low, int high)
	{
		return (int)((high - low + 1) * randomizer.nextFloat()) + low;
	}
}
